package com.suredy.app.project.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.suredy.app.project.constants.DicKey;
import com.suredy.app.project.model.Contract;
import com.suredy.app.project.model.Project;
import com.suredy.app.project.service.ContractSrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.service.DictionarySrv;

/**
 * 合同列表控制器
 * 
 * @author VIVID.G
 * @since 2015-7-30
 * @version v0.1
 */
@Controller
@RequestMapping("/contract/list")
public class ContractListCtrl extends BaseCtrl {

	@Autowired
	private ContractSrv contractSrv;

	@Autowired
	private DictionarySrv dictionarySrv;

	@RequestMapping(value = "/children-contract-list/{projId}", method = RequestMethod.GET)
	public String childrenContract(@PathVariable String projId, Model model) {
		String view = "app/project/form-children-contract";

		if (StringUtils.isBlank(projId))
			return view;

		Project proj = new Project();
		proj.setId(projId);

		Contract search = new Contract();
		search.setProject(proj);
		search.setMainContract(false);

		List<Contract> data = this.contractSrv.readByEntity(search);

		proj.setContracts(new ArrayList<Contract>());
		proj.getContracts().addAll(data);

		model.addAttribute("data", proj);
		model.addAttribute("supplierCompany", this.projSupplierCompany());

		return view;
	}

	private List<String> projSupplierCompany() {
		return this.dictionarySrv.getVals(DicKey.SUPPLIER_COMPANY);
	}

}
